package com.briup.app08.service.impl;

import com.briup.app08.bean.UserInfo;
import com.briup.app08.bean.UserInfoExample;
import com.briup.app08.dao.UserInfoMapper;
import com.briup.app08.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public List<UserInfo> query(String realname,String gender){
        UserInfoExample example=new UserInfoExample();
        if (!StringUtils.isEmpty(realname)){
            example.createCriteria().andRealnameLike("%"+realname+"%");
        }
        if(!StringUtils.isEmpty(gender)){
            example.createCriteria().andGenderEqualTo(gender);
        }

        List<UserInfo> list = userInfoMapper.selectByExample(example);
        return list;
    }

    @Override
    public void saveorupdate(UserInfo userInfo) {

    }

    @Resource
    public void saveOrUpdate(UserInfo userInfo) {
        // 如果userInfo中存在id属性，表示更新；否则保存
        if(userInfo.getId() == null){
            userInfoMapper.insert(userInfo);
        } else {
            userInfoMapper.updateByPrimaryKey(userInfo);
        }
    }

    @Override
    public void deleteById(long id) {
        userInfoMapper.deleteByPrimaryKey(id);
    }
}

